package lesson14;

import java.util.Scanner;


public class Auction {

	int personNumber;
	int carsNumber;	
	Person[] persons;
	Car[] cars;
	
	void open(){
		Scanner sc = new Scanner(System.in);
		persons = new Person[personNumber];
		cars = new Car[carsNumber];
		for (int i = 0; i < personNumber; i++) {
			System.out.println("Enter name of participation number " + (i+1));
			persons[i]= new Person();
			persons[i].name = sc.next();
		}
		
		for (int i = 0; i < carsNumber; i++) {
			System.out.println("Enter model of car number " + (i+1));
			cars[i] = new Car();
			cars[i].model = sc.next();
		}
		
		buyOrNot();
		printCarsAndOwner();
		prinCarsForScrap();
		sc.close();
		
	}
	int madeDesigion(){
		int desigion=(int)Math.round(Math.random()*100);		
		return desigion;
	}
	void buyOrNot(){
		
		for (int i = 0; i < cars.length; i++) {
			int maxWanted = 0;
			int wanted;
			int personIndex = 0 ;
			for (int j = 0; j < persons.length; j++) {
				wanted = madeDesigion();
				if(wanted > maxWanted && persons[j].numberOfOwnedCars>0){
					maxWanted = wanted;
					personIndex = j;
				}
			}
			if(maxWanted<90){			
				cars[i].owner = persons[personIndex];				
				persons[personIndex].numberOfOwnedCars--;
			}
		}
	}
	
	void printCarsAndOwner(){
		for (int i = 0; i < cars.length; i++) {
			if(cars[i].owner!=null){
				System.out.println(cars[i].owner.name+ " buys "+cars[i].model);
			}
		}
	}
	void prinCarsForScrap(){
		for (int i = 0; i < cars.length; i++) {
			if(cars[i].owner==null){
				System.out.println(cars[i].model+" is going for scrap ");
				cars[i]=null;
			}
		}
	}
	
	
}
